package controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.mybeans.dao.DAOException;

import beans.Bookmark;

import model.BookmarkDAO;
import model.Model;

/*
 *  Course: S09-15437 - WEB APPLICATION DEVELOPMENT 
 * 	Name: Jiale Huang
 * 	Andrew ID: jialeh
 * 	Date: 2/23/2009
 *  Assignment: Hw4
 */

public class DelBookmarkAction extends Action {

	BookmarkDAO bookmarkDAO;
	
	public DelBookmarkAction(Model model){
		
		bookmarkDAO = model.getBookmarkDAO();
	}
	
	@Override
	public String getName() {
		return "delBookmark.do";
	}

	@Override
	public String perform(HttpServletRequest request) {
		
		List<String> errors = new ArrayList<String>();
		request.getSession().setAttribute("derrors",errors);
		
		try{
			
			int did = Integer.parseInt( request.getParameter("did") );
			
			if(bookmarkDAO.delBookmark(did)){
				
				String webapp = request.getContextPath();
				return webapp + "/manage.do";
			}else{
				errors.add("Failed to delete");
				String webapp = request.getContextPath();
				return webapp + "/manage.do";
			}
			
		}catch (DAOException e) {
			errors.add(e.toString());
			String webapp = request.getContextPath();
			return webapp + "/manage.do";
		} 
		

	}

}
